<template>
  <div>
    <van-nav-bar
    title="注册"
    left-arrow
    @click-left="$router.back()"
    />
    <van-form @submit="onSubmit" ref="regFrom">
      <van-field
        v-model="mobile"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' },{pattern:/^1[3-9]\d{9}$/,message: '手机号格式错误'}]"
      />
     
      <van-field
        v-model="sms"
        center
          clearable
        label="短信验证码"
        placeholder="请输入短信验证码"
         :rules="[{ required: true, message: '请输入验证码'},{pattern:/^\d{6}$/,message: '验证码错误'}]"
        
        >
        <template #button>
            <van-button size="small" type="info" @click="getfasyzm" native-type="button">发送验证码</van-button>
        </template>

    </van-field>
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' },{validator:(e)=>/^\w{6,12}$/.test(e),message: '密码错误'}]"
    />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" @click="getreg" class="btn">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { Toast } from 'vant';
import { Form } from "vant";
import { Field } from "vant";

export default {
  name: "WorkspaceJsonLogin",
  components: {
    [Form.name]: Form,
    [Field.name]: Field,
    [Toast.name]: Toast
  },
  data() {
    return {
        mobile:'',
        sms:'',
        password:''
    };
  },

  mounted() {},

  methods: {
      showPopup() {
      this.show = true;
    },
    onSubmit(values) {
      console.log('submit', values);
    },
    // 返回上一级
    // onClickLeft(){
    //     this.$router.go(-1)
    // },
    // 发送验证码
    getfasyzm(){
        let {mobile}=this
        this.$http.post('/user/sendSms',{
            mobile:mobile,
            type:'register'
        })
        .then(res=>{
        
        Toast(`验证码已发送到${this.mobile},请注意查收`);
        
        })
    },
    // 注册
    getreg(){
        let {mobile,password,sms}=this
        this.$http.post('/user/reg',{
            username:mobile,
            password:password,
            vercode:sms
        })
        .then(res=>{
          console.log(res);
          this.$toast(res.msg)
          setTimeout(()=>{
              this.$router.go(-1);
          },800)
        })
    }
  },
};
</script>

<style lang="scss" scoped>
.btn {
  background: #2196f3;
  border: none;
  border-radius: 0;
  width: 300px;
  margin-left: 27px;
  margin-top: 10px;
}
</style>